Imports cmplib01

Public Class frmVictoryConditions
    Inherits System.Windows.Forms.Form

    Private ptrGameData As CampaignerGameData
    Private ptrFinal As FinalVictoryCondition

    Private bPopulating As Boolean
    Private bCancelled As Boolean = True

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents lstVictoryConditions As System.Windows.Forms.ListBox
    Friend WithEvents pnlVictoryConditions As System.Windows.Forms.Panel
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents lblExistingConditions As System.Windows.Forms.Label
    Friend WithEvents lblCreateNewVC As System.Windows.Forms.Label
    Friend WithEvents btnAreaObjective As System.Windows.Forms.Button
    Friend WithEvents btnAttrition As System.Windows.Forms.Button
    Friend WithEvents btnChitDestruction As System.Windows.Forms.Button
    Friend WithEvents btnResource As System.Windows.Forms.Button
    Friend WithEvents pnlHorizontalBar As System.Windows.Forms.Panel
    Friend WithEvents btnEdit As System.Windows.Forms.Button
    Friend WithEvents btnDelete As System.Windows.Forms.Button
    Friend WithEvents lblVPsToWin As System.Windows.Forms.Label
    Friend WithEvents numVPsToWin As System.Windows.Forms.NumericUpDown
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmVictoryConditions))
        Me.lstVictoryConditions = New System.Windows.Forms.ListBox
        Me.pnlVictoryConditions = New System.Windows.Forms.Panel
        Me.btnDelete = New System.Windows.Forms.Button
        Me.btnEdit = New System.Windows.Forms.Button
        Me.pnlHorizontalBar = New System.Windows.Forms.Panel
        Me.lblCreateNewVC = New System.Windows.Forms.Label
        Me.lblExistingConditions = New System.Windows.Forms.Label
        Me.btnAreaObjective = New System.Windows.Forms.Button
        Me.btnChitDestruction = New System.Windows.Forms.Button
        Me.btnAttrition = New System.Windows.Forms.Button
        Me.btnResource = New System.Windows.Forms.Button
        Me.btnOK = New System.Windows.Forms.Button
        Me.numVPsToWin = New System.Windows.Forms.NumericUpDown
        Me.lblVPsToWin = New System.Windows.Forms.Label
        Me.pnlVictoryConditions.SuspendLayout()
        CType(Me.numVPsToWin, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'lstVictoryConditions
        '
        Me.lstVictoryConditions.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstVictoryConditions.Location = New System.Drawing.Point(8, 32)
        Me.lstVictoryConditions.Name = "lstVictoryConditions"
        Me.lstVictoryConditions.Size = New System.Drawing.Size(221, 301)
        Me.lstVictoryConditions.TabIndex = 1
        '
        'pnlVictoryConditions
        '
        Me.pnlVictoryConditions.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlVictoryConditions.Controls.Add(Me.btnDelete)
        Me.pnlVictoryConditions.Controls.Add(Me.btnEdit)
        Me.pnlVictoryConditions.Controls.Add(Me.pnlHorizontalBar)
        Me.pnlVictoryConditions.Controls.Add(Me.lblCreateNewVC)
        Me.pnlVictoryConditions.Controls.Add(Me.lblExistingConditions)
        Me.pnlVictoryConditions.Controls.Add(Me.lstVictoryConditions)
        Me.pnlVictoryConditions.Controls.Add(Me.btnAreaObjective)
        Me.pnlVictoryConditions.Controls.Add(Me.btnChitDestruction)
        Me.pnlVictoryConditions.Controls.Add(Me.btnAttrition)
        Me.pnlVictoryConditions.Controls.Add(Me.btnResource)
        Me.pnlVictoryConditions.Location = New System.Drawing.Point(8, 32)
        Me.pnlVictoryConditions.Name = "pnlVictoryConditions"
        Me.pnlVictoryConditions.Size = New System.Drawing.Size(363, 344)
        Me.pnlVictoryConditions.TabIndex = 2
        '
        'btnDelete
        '
        Me.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnDelete.Location = New System.Drawing.Point(240, 208)
        Me.btnDelete.Name = "btnDelete"
        Me.btnDelete.Size = New System.Drawing.Size(75, 23)
        Me.btnDelete.TabIndex = 8
        Me.btnDelete.Text = "Delete"
        '
        'btnEdit
        '
        Me.btnEdit.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnEdit.Location = New System.Drawing.Point(240, 176)
        Me.btnEdit.Name = "btnEdit"
        Me.btnEdit.Size = New System.Drawing.Size(75, 23)
        Me.btnEdit.TabIndex = 7
        Me.btnEdit.Text = "Edit"
        '
        'pnlHorizontalBar
        '
        Me.pnlHorizontalBar.BackColor = System.Drawing.SystemColors.ControlDark
        Me.pnlHorizontalBar.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlHorizontalBar.Location = New System.Drawing.Point(240, 160)
        Me.pnlHorizontalBar.Name = "pnlHorizontalBar"
        Me.pnlHorizontalBar.Size = New System.Drawing.Size(112, 8)
        Me.pnlHorizontalBar.TabIndex = 6
        '
        'lblCreateNewVC
        '
        Me.lblCreateNewVC.Location = New System.Drawing.Point(240, 8)
        Me.lblCreateNewVC.Name = "lblCreateNewVC"
        Me.lblCreateNewVC.Size = New System.Drawing.Size(120, 16)
        Me.lblCreateNewVC.TabIndex = 5
        Me.lblCreateNewVC.Text = "New Victory Condition"
        '
        'lblExistingConditions
        '
        Me.lblExistingConditions.Location = New System.Drawing.Point(16, 8)
        Me.lblExistingConditions.Name = "lblExistingConditions"
        Me.lblExistingConditions.Size = New System.Drawing.Size(160, 16)
        Me.lblExistingConditions.TabIndex = 4
        Me.lblExistingConditions.Text = "Existing Conditions"
        '
        'btnAreaObjective
        '
        Me.btnAreaObjective.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnAreaObjective.Location = New System.Drawing.Point(240, 32)
        Me.btnAreaObjective.Name = "btnAreaObjective"
        Me.btnAreaObjective.Size = New System.Drawing.Size(112, 23)
        Me.btnAreaObjective.TabIndex = 0
        Me.btnAreaObjective.Text = "Area Objective"
        '
        'btnChitDestruction
        '
        Me.btnChitDestruction.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnChitDestruction.Location = New System.Drawing.Point(240, 96)
        Me.btnChitDestruction.Name = "btnChitDestruction"
        Me.btnChitDestruction.Size = New System.Drawing.Size(112, 23)
        Me.btnChitDestruction.TabIndex = 2
        Me.btnChitDestruction.Text = "Chit Destruction"
        '
        'btnAttrition
        '
        Me.btnAttrition.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnAttrition.Location = New System.Drawing.Point(240, 64)
        Me.btnAttrition.Name = "btnAttrition"
        Me.btnAttrition.Size = New System.Drawing.Size(112, 23)
        Me.btnAttrition.TabIndex = 1
        Me.btnAttrition.Text = "Attrition"
        '
        'btnResource
        '
        Me.btnResource.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnResource.Location = New System.Drawing.Point(240, 128)
        Me.btnResource.Name = "btnResource"
        Me.btnResource.Size = New System.Drawing.Size(112, 23)
        Me.btnResource.TabIndex = 3
        Me.btnResource.Text = "Resource"
        '
        'btnOK
        '
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.Location = New System.Drawing.Point(286, 382)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(75, 23)
        Me.btnOK.TabIndex = 4
        Me.btnOK.Text = "OK"
        '
        'numVPsToWin
        '
        Me.numVPsToWin.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.numVPsToWin.Location = New System.Drawing.Point(80, 8)
        Me.numVPsToWin.Name = "numVPsToWin"
        Me.numVPsToWin.Size = New System.Drawing.Size(168, 20)
        Me.numVPsToWin.TabIndex = 5
        Me.numVPsToWin.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'lblVPsToWin
        '
        Me.lblVPsToWin.Location = New System.Drawing.Point(8, 11)
        Me.lblVPsToWin.Name = "lblVPsToWin"
        Me.lblVPsToWin.Size = New System.Drawing.Size(72, 16)
        Me.lblVPsToWin.TabIndex = 6
        Me.lblVPsToWin.Text = "VPs To Win"
        '
        'frmVictoryConditions
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(378, 416)
        Me.Controls.Add(Me.lblVPsToWin)
        Me.Controls.Add(Me.numVPsToWin)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.pnlVictoryConditions)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "frmVictoryConditions"
        Me.Text = "Victory Conditions"
        Me.pnlVictoryConditions.ResumeLayout(False)
        CType(Me.numVPsToWin, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Sub New(ByRef GameData As CampaignerGameData)
        Me.New()

        ptrGameData = GameData
        ListExistingVictoryConditions()
    End Sub

    Public ReadOnly Property Cancelled() As Boolean
        Get
            Return bCancelled
        End Get
    End Property

    Private Sub ListExistingVictoryConditions()
        Dim vc As IVictoryCondition
        Dim noFinal As Boolean = True   ' we need to make sure there is a final victory condition item. It doesn't
        ' get added to the list though.
        Dim totalVPValue As Integer

        bPopulating = True

        lstVictoryConditions.Items.Clear()

        For Each vc In ptrGameData.selectedFaction.FactionVictoryConditions
            If TypeOf vc Is FinalVictoryCondition Then
                noFinal = False
                ptrFinal = vc
            Else
                lstVictoryConditions.Items.Add(vc)
                totalVPValue += vc.VPValue
            End If
        Next

        If noFinal Then
            Dim final As New FinalVictoryCondition
            final.AutomaticVictory = True
            final.VPValue = totalVPValue
            ptrGameData.selectedFaction.FactionVictoryConditions.Add(final)
            ptrFinal = final
        End If

        ' show the vp value to win
        numVPsToWin.Value = ptrFinal.VPValue

        bPopulating = False

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        ptrFinal.VPValue = numVPsToWin.Value
        bCancelled = False
        Me.Hide()
    End Sub

    Private Sub btnAreaObjective_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAreaObjective.Click
        ' adds a new areaobjective victory condition
        Try
            Dim NewAOVC As New AreaObjectiveVictoryCondition

            NewAOVC.VictoryConditionName = "New Area Objective VC"
            NewAOVC.Description = "Enter a description"
            NewAOVC.AutomaticVictory = False
            NewAOVC.VPValue = 5

            Dim fAOVCProperties As New CVCForms.frmAreaObjectiveProperties(NewAOVC, ptrGameData)
            fAOVCProperties.ShowDialog()

            If fAOVCProperties.Cancelled Then Exit Sub

            ' add this entry
            ptrGameData.selectedFaction.FactionVictoryConditions.Add(NewAOVC)
            ListExistingVictoryConditions()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If lstVictoryConditions.SelectedItem Is Nothing Then Exit Sub

        Dim VC As IVictoryCondition = lstVictoryConditions.SelectedItem

        Select Case VC.VCType
            Case Is = VictoryConditionClass_cgd.AreaObjective
                Dim fAOVCProperties As New CVCForms.frmAreaObjectiveProperties(VC, ptrGameData)
                fAOVCProperties.ShowDialog()
            Case Is = VictoryConditionClass_cgd.AttritionObjective
                Dim fAVCProperties As New CVCForms.frmAttritionVictoryCondition(VC, ptrGameData)
                fAVCProperties.ShowDialog()
            Case Is = VictoryConditionClass_cgd.ChitDestruction
                Dim fAOVCProperties As New CVCForms.frmAreaObjectiveProperties(VC, ptrGameData)
                fAOVCProperties.ShowDialog()
            Case Is = VictoryConditionClass_cgd.Resource
                Dim fRVC As New CVCForms.frmResourceVictoryCondition(VC, ptrGameData)
                fRVC.ShowDialog()
        End Select

        ' just incase the items name has been edited...
        ListExistingVictoryConditions()

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If lstVictoryConditions.SelectedItem Is Nothing Then Exit Sub

        If MessageBox.Show("Are you sure you wish to delete this Victory Condition?", "Confirm Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            ptrGameData.selectedFaction.FactionVictoryConditions.Remove(lstVictoryConditions.SelectedItem)
            ListExistingVictoryConditions()
        End If

    End Sub

    Private Sub btnChitDestruction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChitDestruction.Click
        Dim NewCDVC As New ChitDestructionVictoryCondition
        Try
            NewCDVC.VictoryConditionName = "New Area Objective VC"
            NewCDVC.Description = "Enter a description"
            NewCDVC.AutomaticVictory = False
            NewCDVC.VPValue = 5

            Dim fAOVCProperties As New CVCForms.frmAreaObjectiveProperties(NewCDVC, ptrGameData)
            fAOVCProperties.ShowDialog()

            If fAOVCProperties.Cancelled Then Exit Sub

            ' add this entry
            ptrGameData.selectedFaction.FactionVictoryConditions.Add(NewCDVC)
            ListExistingVictoryConditions()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub btnAttrition_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttrition.Click
        Dim newAVC As New AttritionVictoryCondition

        Try
            newAVC.VictoryConditionName = "New Attrition Victory Condition"
            newAVC.Description = "Enter a description"
            newAVC.AutomaticVictory = False
            newAVC.VPValue = 5

            Dim fAVC As New CVCForms.frmAttritionVictoryCondition(newAVC, ptrGameData)
            fAVC.ShowDialog()
            If fAVC.Cancelled Then Exit Sub

            ptrGameData.selectedFaction.FactionVictoryConditions.Add(newAVC)
            ListExistingVictoryConditions()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub btnResource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResource.Click
        Dim newRVC As New ResourceVictoryCondition

        Try
            newRVC.VictoryConditionName = "New Resource Victory Condition"
            newRVC.Description = "Enter a description"
            newRVC.AutomaticVictory = False
            newRVC.VPValue = 5
            newRVC.AmountNeeded = 500
            Dim fRVC As New CVCForms.frmResourceVictoryCondition(newRVC, ptrGameData)
            fRVC.ShowDialog()

            ptrGameData.selectedFaction.FactionVictoryConditions.Add(newRVC)
            ListExistingVictoryConditions()
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try

    End Sub

    Private Sub numVPsToWin_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles numVPsToWin.ValueChanged
        If Not ptrFinal Is Nothing Then ptrFinal.VPValue = numVPsToWin.Value

    End Sub
End Class
